//
//  WBNetworkManager.m
//  TransWB
//
//  Created by 孝飞王 on 2016/12/30.
//  Copyright © 2016年 孝飞王. All rights reserved.
//

#import "WBNetworkManager.h"

@interface WBNetworkManager()


@end
@implementation WBNetworkManager



+ (instancetype)shareManager{
    
    static WBNetworkManager *manager;
    static dispatch_once_t  onceToken;
    dispatch_once(&onceToken,^{
        
        manager = [[WBNetworkManager alloc]init];
        //[manager.responseSerializer.acceptableContentTypes setByAddingObject:@" text/plain"];
       manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
        
    });
    
    return manager;
}


- (void)tokenRequestHttpMethod:(WBHTTPMethod)method urlString:(NSString*)urlString parameters:(NSDictionary*)parameters completion:(void(^)(id responseObject, BOOL isSuccess))completion{
    
    if ([self.userAccount.access_token isEqualToString:@""] || !self.userAccount.access_token) {
 
        self.isLogin = NO;
#warning token past
        
        //发送通知提示让登录
        [[NSNotificationCenter defaultCenter] postNotificationName:ShouldLoginSuccess object:@"past"];
        
        NSLog(@"你没有 token");
        completion(nil,NO);
        return;
    }
    self.isLogin = YES;
    //拼接字典
    NSMutableDictionary *paramter = [NSMutableDictionary dictionaryWithDictionary:parameters];
    paramter[@"access_token"] = self.userAccount.access_token;
   
    //调用原来的方法 
    [self requestHTTPMethod:method urlString:urlString parameters:paramter completion:completion];
    
}





//使用一个参数来封装函数
- (void)requestHTTPMethod:(WBHTTPMethod)method urlString:(NSString*)urlString parameters:(NSDictionary*)parameters completion:(void(^)(id responseObject, BOOL isSuccess))completion{
    
    //成功的回调
    void(^success)() = ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject){
        completion(responseObject,YES);
    };
    
    //失败的调用
    void(^failure)() = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
        
        //设置网络错误的处理
        NSLog(@"网络请求错误%@",error);
        NSHTTPURLResponse *httpRespose  = (NSHTTPURLResponse*)(task.response);
        
        if (httpRespose.statusCode == 403) {
#warning Token Past
            
            //发送通知让登录
            [[NSNotificationCenter defaultCenter] postNotificationName:ShouldLoginSuccess object:@"lazy"];
            
            NSLog(@"token 过期");
        }
        completion(nil,NO);
    };
    
    if (method == GET) {
        
        [[WBNetworkManager shareManager] GET:urlString parameters:parameters progress:nil success:success failure:failure];
    }else{
    
        [[WBNetworkManager shareManager] POST:urlString parameters:parameters progress:nil success:success failure:failure];
    }
}














#pragma mark - LazyLoad
- (WBUserModel *)userAccount{
    
    if (_userAccount == nil) {
        _userAccount = [[WBUserModel alloc]init];
    }
    return _userAccount;
}

- (BOOL)isLogin{
    
    //判断是不是有账户或者密码
    return ([self.userAccount.access_token isEqualToString:@""] || !self.userAccount.access_token) ? NO:YES;
}


@end
